<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GuanggaoController extends Controller
{
    //
    public function add()
    {
    	$title = '广告位添加';
    	return view('admin.guanggao.add',['title' => $title]);
    }

    public function insert(Request $request)
    {
    	$data = $request -> except('_token');

    	//设置限制条件
    	$this -> validate($request, [
    		            	'name' => 'required|max:30',
    		             	 'url' => 'required',
    		],[
    	 		   'name.required' => '广告名称不能为空',
    	   		    'name.max' => '广告名称不能超过30位',
    			    'url.required' => '广告链接地址不能为空',

    		]);

    	//处理pic
        if($request -> hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                // echo $suffix;
                //随机文件名
                $fileName = time().mt_rand(100000, 999999).'.'.$suffix;
                // $path = date('Ymd');
                $path = './uploads/guanggao';
                $request -> file('pic') -> move($path,$fileName);

                $data['pic'] = trim($path.'/'.$fileName,'.');
                // echo $data['pic'];    
            }
        }


        // 执行添加
    	$res = DB::table('guanggao') -> insert($data);

    	//判断
		if($res)
		{
			return redirect('/admin/guanggao/index') -> with('success', '添加成功');
		}else
		{
			return back() -> with('error', '添加失败');
		}
    }

    //广告列表页
    public function index()
    {
    	$title = '列表页';
    	$data = DB::table('guanggao') -> get();
    	// dd($data);
    	return view('admin/guanggao/index',['title' => $title,'data' => $data]);
    }

    //编辑页面
    public function edit($id)
    {
    	$title = '广告编辑页面';
    	$data = DB::table('guanggao')
            -> where('id','=',$id) 
            -> first();
        return view('admin/guanggao/edit',['title' => $title,'data' => $data]);
    }

    //执行页面更新
    public function update(Request $request)
    {
    	//
    	$data = $request -> except('_token','id','oldpic');
    	// dd($data);

    	$id = $request -> input('id');

    	//设置限制条件
    	$this -> validate($request, [
    		            	'name' => 'required|max:30',
    		             	 'url' => 'required',
    		],[
    	 		   'name.required' => '广告名称不能为空',
    	   		    'name.max' => '广告名称不能超过30位',
    			    'url.required' => '广告链接地址不能为空',

    		]);

    	//获取老图片地址
        $oldpath = '.'.$request -> input('oldpic');

    	 //处理pic
        if($request -> hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request 
                    -> file('pic') 
                    -> getClientOriginalExtension();
                // echo $suffix;
                //随机文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                // $path = date('Ymd');
                $path = './uploads/guanggao';
                $request -> file('pic') -> move($path,$fileName);

                $data['pic'] = trim($path.'/'.$fileName,'.');
                // echo $data['pic']; 

                //判断有老图片删除
                if(file_exists($oldpath))
                {
                    unlink($oldpath);
                }   
            }
        }

        //执行添加数据
        $res = DB::table('guanggao') 
            -> where('id',$id) 
            -> update($data);

        //判断
        if($res)
        {
            return redirect('/admin/guanggao/index') -> with(['success' => '更新成功']);
        }else
        {
            return back() -> with(['error' => '更新失败或未修改数据']);
        }

    }


    //执行删除
    public function delete($id)
    {
    	$res = DB::table('guanggao') 
            -> where('id',$id) 
            -> delete();
    	if($res)
        {
            return redirect('/admin/guanggao/index') -> with(['success' => '删除成功']);
        }else
        {
            return back() -> with(['error' => '删除失败']);
        }
    }
}
